odoo.define('web.numpril', function(require) {
    "use strict";

    var core = require('web.core');
    var widget = require('web.form_widgets');
    var Widget = require('web.Widget');
    var FormView = require('web.FormView');
    var common = require('web.form_common');
    var Model = require('web.Model');
    var card_id = 0;
    var card_show;
    var QWeb = core.qweb;
    var _lt = core._lt;

    var Consumption = Widget.extend({
        // sleep:function(numberMillis){
        //     var now = new Date(); 
        //     var exitTime = now.getTime() + numberMillis; 
        //     while (true) { 
        //     now = new Date(); 
        //     if (now.getTime() > exitTime) 
        //     return; 
        //     } 
        // },
        start: function () {
            var self = this;
            
            self.$el.html(QWeb.render("display_wecome_consump"));
        

            core.bus.on('barcode_scanned', this, function (barcode) {
                // this.scan(barcode);
                //console.log(barcode)
                self.barcode = barcode;
                card_id = barcode;
                card_show = card_id.slice(0,4)+"······"+card_id.slice(7);

                var hr_employee = new Model('numpril.card');
                hr_employee.query(['card_balance', 'name'])
                    .filter([['name', '=', self.barcode]])
                    .all()
                    .then(function (res) {
                        if(res.length == 0 | res.length > 1){
                            alert('no res')
                        }else{
                        console.log(self.barcode)
                        console.log(res[0]['card_balance'])
                        self.barcode = card_show
                        self.balance = res[0]['card_balance'];
                        //console.log(self.employee)
                        self.$el.html(QWeb.render("Consumption_1", {widget: self}));
                        }
                    });

            });
            return this._super.apply(this, arguments);
        },
        events:{
            "click .deal_consump_value":"button_click",
            "click .deal_password_value":"deal_passw",
        },
       
        button_click:function(){
            var self = this;
            var consump_input = this.$el.find('.id_for_consump');
            var confirm = true;
            var password = true;
            if(consump_input[0].value<0){
                alert("消费数字有误！");
                confirm = false;
            }
            else{
                console.log(consump_input[0].value)
                self.consumpSum = consump_input[0].value;
                var model = new Model('numpril.consumption')
                var card_password =new Model('numpril.card');
                card_password.query(['card_password','name'])
                        .filter([['name','=',card_id]])
                        .all()
                        .then(function(res){
                            console.log("mimashi:")
                            console.log(res[0].card_password);
                            if(res[0].card_password){
                                console.log("需要密码")
                                self.$el.html(QWeb.render("password_main"));
                                }
                            else
                                model.call("add_comsumption",[],{arg1:card_id,arg2:self.consumpSum})
                                    .then(function(result){
                                //-----------------------------------------------
                                    var date = new Date();
                                    var seperator1 = "-";
                                    var seperator2 = ":";
                                    var month = date.getMonth() + 1;
                                    var strDate = date.getDate();
                                    if (month >= 1 && month <= 9) {
                                        month = "0" + month;
                                    }
                                    if (strDate >= 0 && strDate <= 9) {
                                        strDate = "0" + strDate;
                                    }
                                    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                                            + " " + date.getHours() + seperator2 + date.getMinutes()
                                            + seperator2 + date.getSeconds();
                            //-----------------------------------------------
                                    self.username = result[0];
                                    // console.log("33")
                                    // console.log(result)
                                    self.balance = result[1];
                                    self.consump = self.consumpSum;
                                    self.barcode = card_show;
                                    self.time = currentdate;
                                    self.$el.html(QWeb.render("consump_print", {widget: self})); 
                                   
                                    //setTimeout(self.$el.html(QWeb.render("display_wecome_consump")),3500);
                                    self.do_action({
                                        type: 'ir.actions.client',
                                        tag: 'einfo_print_lodop_pos',
                                        params: {
                                            data: self.$('.o_hr_attendance_kiosk_mode').html()
                                        }
                                    });
                                    setTimeout(function(){ self.$el.html(QWeb.render("display_wecome_consump"));},3000);
                            })
                      
                        })

                    }
            
    
        },
        deal_passw:function(){
            var self = this;
            var password_input = this.$el.find(".id_for_password");
            var password = password_input[0].value;
            
            var model = new Model ('numpril.card');
            var arg1,arg2;

            model.call("passw_comfirm",[],{arg1:card_id,arg2:password})
                .then(function(result){
                    if(!result){
                        alert("密码错误");
                    }else{
                        var model = new Model('numpril.consumption');
                        model.call("add_comsumption",[],{arg1:card_id,arg2:self.consumpSum})
                        .then(function(result){
                            var date = new Date();
                            var seperator1 = "-";
                            var seperator2 = ":";
                            var month = date.getMonth() + 1;
                            var strDate = date.getDate();
                            if (month >= 1 && month <= 9) {
                                month = "0" + month;
                            }
                            if (strDate >= 0 && strDate <= 9) {
                                strDate = "0" + strDate;
                            }
                            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                                    + " " + date.getHours() + seperator2 + date.getMinutes()
                                    + seperator2 + date.getSeconds();

                            console.log("33")
                            console.log(result)
                            self.username = result[0];
                            self.balance = result[1];
                            self.consump = self.consumpSum;
                            self.barcode = card_show;
                            self.time = currentdate;
                            self.$el.html(QWeb.render("consump_print", {widget: self})); 
                            self.do_action({
                                type: 'ir.actions.client',
                                tag: 'einfo_print_lodop_pos',
                                params: {
                                    data: self.$('.o_hr_attendance_kiosk_mode').html()
                                }
                            });
                            //setTimeout(self.$el.html(QWeb.render("display_wecome_consump")),3500);
                            setTimeout(function(){ self.$el.html(QWeb.render("display_wecome_consump"));},3000);
                        
                    });
                    }
                })

        },
      
    });
    console.log("1")
    core.action_registry.add('numpril_consumption', Consumption);
    
    

    var Rechargement = Widget.extend({
        
         start: function () {
             var self = this;
             
             self.$el.html(QWeb.render("display_wecome_recharge"));

             core.bus.on('barcode_scanned', this, function (barcode) {
                 // this.scan(barcode);
                 //console.log(barcode)
                 //self.barcode = barcode;
                 card_id = barcode;
                 card_show = card_id.slice(0,4)+"······"+card_id.slice(7);
                 var hr_employee = new Model('numpril.card');
                 hr_employee.query(['card_balance', 'name'])
                     .filter([['name', '=', barcode]])
                     .all()
                     .then(function (res) {
                         // if (_.isEmpty(res) ) {
                         //     self.$('.o_hr_attendance_employee').append(_t("Error : Could not find employee linked to user"));
                         //     return;
                         // };
                         console.log(res)
                         if(res.length == 0 | res.length > 1){
                             alert('no res')
                         }else{
                         self.barcode = card_show
                         console.log(self.barcode)
                         console.log(res[0]['card_balance'])
                         self.balance = res[0]['card_balance'];
                         //console.log(self.employee)
                         self.$el.html(QWeb.render("rechargement_1", {widget: self}));
                         }
                     });
                 
 
 
                 //self.$el.html(QWeb.render("Consumption_1",{widget: self}));
             });
             return this._super.apply(this, arguments);
         },
         events:{
             "click .deal_recharge_value":"button_click",
         },
         button_click:function(){
             var self = this
             var rechar_input = this.$el.find('.id_for_recharge');
             var confirm = true;
             if(rechar_input[0].value<0){
                 alert("消费数字有误！");
                 confirm = false;
             }
             console.log(rechar_input[0].value)
             var aa = rechar_input[0].value;
             var model = new Model('numpril.recharge');
             var arg1,arg2;
             // console.log(typeof(aa))
             // console.log(typeof(card_id))
             // console.log(card_id)
             if(confirm){
             model.call("add_recharge",[],{arg1:card_id,arg2:aa})
                 .then(function(result){

                    //-----------------------------------------------
                    var date = new Date();
                    var seperator1 = "-";
                    var seperator2 = ":";
                    var month = date.getMonth() + 1;
                    var strDate = date.getDate();
                    if (month >= 1 && month <= 9) {
                        month = "0" + month;
                    }
                    if (strDate >= 0 && strDate <= 9) {
                        strDate = "0" + strDate;
                    }
                    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                            + " " + date.getHours() + seperator2 + date.getMinutes()
                            + seperator2 + date.getSeconds();
                    //-----------------------------------------------
                     console.log("33")
                     console.log(result)
                     self.username = result[0];
                     self.balance = result[1];
                     self.recharge = rechar_input[0].value;
                     self.barcode = card_show;
                     self.time = currentdate;
                     self.$el.html(QWeb.render("recharge_print", {widget: self}));
                     self.do_action({
                        type: 'ir.actions.client',
                        tag: 'einfo_print_lodop_pos',
                        params: {
                            data: self.$('.o_hr_attendance_kiosk_mode').html()
                        }
                    });
                     //self.$el.html(QWeb.render("rechargement_2"),{widget:self});
                     setTimeout(function(){ self.$el.html(QWeb.render("display_wecome_recharge"));},2000);
                     
                 });
             }   
         },
 
     });
     console.log("3")
     core.action_registry.add('numpril_recharge', Rechargement);
     console.log("4")
  
})   

